package com.cp.admin.core.util;

import com.cp.admin.api.utils.DateUtils;
import com.cp.core.util.DateUtil;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class CpDateWebSocket implements WebSocketHandler {

    public static String webUrl2 = "http://www.baidu.com";//百度
    public static String webUrl3 = "http://www.taobao.com";//淘宝

    private String cqssc;

    private String xjssc;

    private String bjpk10;

    private String klssc;

    private String klsc;

    private String klft;


    //建立连接后的操作
    @Override
    public void afterConnectionEstablished(WebSocketSession webSocketSession) throws Exception {

    }

    @Override
    public void handleMessage(WebSocketSession webSocketSession, WebSocketMessage<?> webSocketMessage) throws Exception {

    }

    @Override
    public void handleTransportError(WebSocketSession webSocketSession, Throwable throwable) throws Exception {

    }

    //关闭连接的操作
    @Override
    public void afterConnectionClosed(WebSocketSession webSocketSession, CloseStatus closeStatus) throws Exception {

    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }


    public static void main(String[] args) {
        long totalSeconds = 60 * 18 * 1;


        String dateStr = As.getWebsiteDatetime("http://www.baidu.com");

        System.out.println(dateStr);

        String endStr1 = dateStr.substring(0,14) + "08:00";
        String endStr2 = dateStr.substring(0,14) + "28:00";
        String endStr3 = dateStr.substring(0,14) + "48:00";

        String endStr4 = dateStr.substring(0,14) + "10:00";
        String endStr5 = dateStr.substring(0,14) + "30:00";
        String endStr6 = dateStr.substring(0,14) + "50:00";


        if(DateUtils.strToDate(dateStr).getTime() < DateUtils.strToDate(endStr4).getTime()){
            totalSeconds = DateUtils.strToDate(endStr1).getTime() - DateUtils.strToDate(dateStr).getTime();
            totalSeconds = totalSeconds/1000;
        }

        if(DateUtils.strToDate(dateStr).getTime() > DateUtils.strToDate(endStr4).getTime() &&
                DateUtils.strToDate(dateStr).getTime() < DateUtils.strToDate(endStr5).getTime()){
            totalSeconds = DateUtils.strToDate(endStr2).getTime() - DateUtils.strToDate(dateStr).getTime();
            totalSeconds = totalSeconds/1000;
        }

        if(DateUtils.strToDate(dateStr).getTime() > DateUtils.strToDate(endStr5).getTime() &&
                DateUtils.strToDate(dateStr).getTime() < DateUtils.strToDate(endStr6).getTime()){
            totalSeconds = DateUtils.strToDate(endStr2).getTime() - DateUtils.strToDate(dateStr).getTime();
            totalSeconds = totalSeconds/1000;
        }

        if(DateUtils.strToDate(dateStr).getTime() > DateUtils.strToDate(endStr6).getTime()){
            Date d = DateUtils.addHour(new Date(DateUtils.strToDate(dateStr).getTime()),1);

            if(DateUtils.strToDate(dateStr).getTime() < d.getTime()){
                totalSeconds = d.getTime() - DateUtils.strToDate(dateStr).getTime();
                totalSeconds = totalSeconds/1000;
            }

        }

        As.initData(totalSeconds);

        new Timer().schedule(new TimerTask() {
            public void run() {
                if (As.secondNotAlready) {
                    As.startCount();
                } else {
                    cancel();
                }

            }
        }, 0, 1000);
    }

}
